package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.enums.ordercenter.OrderCategoryEnum;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.SetOrderTag;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.enums.OrderStatus;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomUpdateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderLineAtomUpdateRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.alibaba.citrus.cr.common.constant.OrderFeaturesKey.*;

/**
 * @author zhangxiaorui
 * @ClassName OrderLineConvert
 * @description: TODO
 * @date 2023/05/17 15:27
 * @version: 1.0
 */
@Service
public class OrderAtomUpdateRequestToOrderConvert {

    private static final Logger log = LoggerFactory.getLogger(OrderAtomUpdateRequestToOrderConvert.class);

    /**
     * 将要修改的字段赋值给 更新对象
     * 如有其他字段要更新，在子类重新本方法
     */
    public void convert(Order dbOrder, OrderAtomUpdateRequest request, String warehouseCode) {
        // 所有状态都可以修改的字段
        if (StringUtils.isNotBlank(request.getDeliveryMethodCode())) {
            dbOrder.setTransportModeCode(request.getDeliveryMethodCode());
            dbOrder.setTransportModeName(request.getDeliveryMethodName());
        }
        if (OrderCategoryEnum.COMPANY.equals(OrderCategoryEnum.of(dbOrder.getOrderCategoryCode()))) {
            dbOrder.setRemark(request.getNote());
            dbOrder.setCreateReason(request.getOrderReasonCode());
            dbOrder.setCreateReasonName(request.getOrderReasonName());
        } else {
            if (StringUtils.isNotBlank(request.getNote())) {
                dbOrder.setRemark(request.getNote());
            }
            if (StringUtils.isNotBlank(request.getOrderReasonCode())) {
                dbOrder.setCreateReason(request.getOrderReasonCode());
                dbOrder.setCreateReasonName(request.getOrderReasonName());
            }
        }
        if(StringUtils.isNotBlank(request.getCostCenterCode())){
            dbOrder.setCostCenter(request.getCostCenterCode());
        }
        if (StringUtils.isNotBlank(request.getCostCenterName())) {
            dbOrder.setCostCenterName(request.getCostCenterName());
        }
        if (StringUtils.isNotBlank(request.getFulfillmentPriority())) {
            dbOrder.setFulfillmentPriority(request.getFulfillmentPriority());
        }
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(request.getOrderBizTag())) {
            dbOrder.setOrderBizTag(String.join(",", request.getOrderBizTag()));
        }
        JSONObject features = JSON.parseObject(Optional.ofNullable(dbOrder.getFeatures()).orElse(""));
        if (StringUtils.isNotBlank(request.getKaArea())) {
            features.put(KA_AREA, request.getKaArea());
        }
        if (StringUtils.isNotBlank(request.getKaSystem())) {
            features.put(KA_SYSTEM, request.getKaSystem());
        }
        if (StringUtils.isNotBlank(request.getSourceOrderTime())) {
            features.put(SOURCE_ORDER_TIME, request.getSourceOrderTime());
        }
        dbOrder.setFeatures(features.toJSONString());
        if (StringUtils.isNotBlank(request.getSalesChannelCode())) {
            dbOrder.setSaleChannelOutCode(request.getSalesChannelCode());
        }
        if (StringUtils.isNotBlank(request.getSalesChannelName())) {
            dbOrder.setSaleChannelName(request.getSalesChannelName());
        }
        if (StringUtils.isNotBlank(request.getSalesDepartmentCode())) {
            dbOrder.setDepartmentOutCode(request.getSalesDepartmentCode());
        }
        if (StringUtils.isNotBlank(request.getSalesDepartmentName())) {
            dbOrder.setDepartmentName(request.getSalesDepartmentName());
        }

        // 待提交或者(退货订单)待推送状态可以修改的字段
        if (OrderStatus.BEFORE_SUBMIT.getCode().equals(dbOrder.getStatus())
                || (SetOrderTag.RETURN.equals(dbOrder.getOrderTag()) && OrderStatus.STAY_PUSH.getCode().equals(dbOrder.getStatus()))) {
            if (StringUtils.isNotBlank(request.getExpectedDeliveryDate())) {
                dbOrder.setCustomerExpectDate(DateUtils.string2Date(request.getExpectedDeliveryDate()));
            }
            if (CollectionUtils.isNotEmpty(request.getOrderDetails())) {
                OrderLineAtomUpdateRequest tmpChild = request.getOrderDetails().get(0);
                dbOrder.setWareHouseOutCode(tmpChild.getShippingWarehouseCode());
                dbOrder.setWareHouseCode(warehouseCode);
                dbOrder.setWareHouseName(tmpChild.getShippingWarehouseName());
            }
            if (StringUtils.isNotBlank(request.getReceiveWarehouseCode())) {
                dbOrder.setReceiveWareHouseOutCode(request.getReceiveWarehouseCode());
            }
            if (StringUtils.isNotBlank(request.getReceiveFactoryCode())) {
                dbOrder.setReceiveFactoryCode(request.getReceiveFactoryCode());
            }
            if (StringUtils.isNotBlank(request.getReceiveStockCode())) {
                dbOrder.setReceiveStockCode(request.getReceiveStockCode());
            }
            if (StringUtils.isNotBlank(request.getShipToPartyCode())) {
                dbOrder.setReceiveAddressCode(request.getShipToPartyCode());
            }
            if (StringUtils.isNotBlank(request.getShipToPartyName())) {
                dbOrder.setReceiveDetailAddress(request.getShipToPartyName());
            }
            if (StringUtils.isNotBlank(request.getBizFeatures())) {
                dbOrder.setBizFeatures(request.getBizFeatures());
            }
            if (StringUtils.isNotBlank(request.getOrderReasonCode())) {
                dbOrder.setCreateReason(request.getOrderReasonCode());
                dbOrder.setCreateReasonName(request.getOrderReasonName());
            }
            if (StringUtils.isNotBlank(request.getFulfillmentTypeCode())) {
                dbOrder.setFulfillmentTypeCode(request.getFulfillmentTypeCode());
            }
            if (StringUtils.isNotBlank(request.getAutoFulfillment())) {
                dbOrder.setAutoFulfillment(request.getAutoFulfillment());
            }
        }
    }

    public void convertOrder(Order dbOrder, OrderAtomUpdateRequest request) {
        if (StringUtils.isNotBlank(request.getDeliveryMethodCode())) {
            dbOrder.setTransportModeCode(request.getDeliveryMethodCode());
            dbOrder.setTransportModeName(request.getDeliveryMethodName());
        }
        if (StringUtils.isNotBlank(request.getOrderReasonCode())) {
            dbOrder.setCreateReason(request.getOrderReasonCode());
            dbOrder.setCreateReasonName(request.getOrderReasonName());
        }
        if (StringUtils.isNotBlank(request.getNote())) {
            dbOrder.setRemark(request.getNote());
        }
        if(StringUtils.isNotBlank(request.getCostCenterCode())){
            dbOrder.setCostCenter(request.getCostCenterCode());
        }
        if (StringUtils.isNotBlank(request.getCostCenterName())) {
            dbOrder.setCostCenterName(request.getCostCenterName());
        }
        if (StringUtils.isNotBlank(request.getBizFeatures())) {
            dbOrder.setBizFeatures(request.getBizFeatures());
        }
    }

}
